package com.example.day15_7_15_1.fragment;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.day15_7_15_1.R;
import com.example.day15_7_15_1.adapter.InfoRclAdapter;
import com.example.day15_7_15_1.bean.Bean;
import com.example.day15_7_15_1.net.ApiService;
import com.example.day15_7_15_1.util.HttpUtil;

import io.reactivex.schedulers.Schedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subscribers.ResourceSubscriber;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
    RecyclerView rcl;
    private InfoRclAdapter infoRclAdapter;


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        iniData();
    }

    private void iniData() {
        HttpUtil.getInstance().getService(ApiService.dataUrl)
                .getData()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new ResourceSubscriber<Bean>() {
                    @Override
                    public void onNext(Bean bean) {
                        infoRclAdapter.setList(bean.getData().getDatas());
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initView(View view) {
        rcl = view.findViewById(R.id.rcl);
        Context context = getContext();
        this.rcl.setLayoutManager(new LinearLayoutManager(context));
        infoRclAdapter = new InfoRclAdapter(context);
        this.rcl.setAdapter(infoRclAdapter);
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}
